package router

import (
	"github.com/gin-gonic/gin"
	"strconv"
	"tourism-app/client/client"
	"tourism-app/client/enit"
	"tourism-app/common/proto/user"
)

func ScenicToEs(c *gin.Context) {
	es, err := client.ScenicToEs(c, &user.ScenicToEsReq{})
	if err != nil {
		c.JSON(200, gin.H{
			"code": 1000,
			"data": err.Error(),
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 0,
		"data": es,
	})
}
func SearchScenic(c *gin.Context) {
	var data enit.SearchScenic
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(200, gin.H{
			"code": 1000,
			"data": err.Error(),
		})
		return
	}
	scenic, err := client.SearchScenic(c, &user.SearchScenicReq{KeyWord: data.KeyWord})
	if err != nil {
		c.JSON(200, gin.H{
			"code": 1000,
			"data": err.Error(),
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 0,
		"data": scenic,
	})

}
func ScenicItem(c *gin.Context) {
	var data enit.ScenicItem
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(200, gin.H{
			"code": 1000,
			"data": err.Error(),
		})
		return
	}
	scenic, err := client.ScenicItem(c, &user.ScenicItemReq{Id: data.Id})
	if err != nil {
		c.JSON(200, gin.H{
			"code": 1000,
			"data": err.Error(),
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 0,
		"data": scenic,
	})
}
func Login(c *gin.Context) {
	var data enit.Login
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(200, gin.H{
			"code": 1000,
			"data": err.Error(),
		})
		return
	}
	login, err := client.Login(c, &user.LoginReq{
		Mobile:   data.Mobile,
		Password: data.Password,
	})
	if err != nil {
		c.JSON(200, gin.H{
			"code": 1000,
			"data": err.Error(),
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 0,
		"data": login,
	})
}
func CreateOrder(c *gin.Context) {
	var data enit.CreateOrder
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(200, gin.H{
			"code": 1000,
			"data": err.Error(),
		})
		return
	}
	userId := c.GetUint("userId")

	order, err := client.CreateOrder(c, &user.CreateOrderReq{
		UserId:      int64(userId),
		ScenicId:    data.ScenicId,
		AdmissionId: data.AdmissionId,
		Num:         data.Num,
		InsureId:    data.InsureId,
	})
	if err != nil {
		c.JSON(200, gin.H{
			"code": 1000,
			"data": err.Error(),
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 0,
		"data": order,
	})
}
func RealName(c *gin.Context) {
	var data enit.RealName
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(200, gin.H{
			"code": 1000,
			"data": err.Error(),
		})
		return
	}
	userId := c.GetUint("userId")
	name, err := client.RealName(c, &user.RealNameReq{
		Name:   data.Name,
		IdCard: data.IdCard,
		UserId: int64(userId),
	})
	if err != nil {
		c.JSON(200, gin.H{
			"code": 1000,
			"data": err.Error(),
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 0,
		"data": name,
	})

}
func AliPay(c *gin.Context) {

	ordersn := c.GetString("orderSn")
	status := c.GetString("status")
	atoi, err := strconv.Atoi(status)
	if err != nil {
		return
	}
	statuss, err := client.UpdateOrderStatus(c, &user.UpdateOrderStatusReq{
		OrderSn: ordersn,
		Status:  int64(atoi),
	})
	if err != nil {
		return
	}
	c.JSON(200, gin.H{
		"code": 0,
		"data": statuss,
	})
}
func OrderList(c *gin.Context) {
	userId := c.GetUint("userId")
	list, err := client.OrderList(c, &user.OrderListReq{UserId: int64(userId)})
	if err != nil {
		c.JSON(200, gin.H{
			"code": 1000,
			"data": err.Error(),
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 0,
		"data": list,
	})
}
func Refund(c *gin.Context) {
	var data enit.Refund
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(200, gin.H{
			"code": 1000,
			"data": err.Error(),
		})
		return
	}
	userId := c.GetUint("userId")
	refund, err := client.Refund(c, &user.RefundReq{
		UserId:  int64(userId),
		OrderSn: data.OrderSn,
		Status:  data.Status,
	})
	if err != nil {
		c.JSON(200, gin.H{
			"code": 1000,
			"data": err.Error(),
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 0,
		"data": refund,
	})
}
